//package com.le.sarrs.web.handler.piece;
//
//import com.google.common.collect.Lists;
//import com.le.sarrs.service.MessageClientService;
//import com.le.sarrs.web.event.TranEvent;
//import net.hexi.sword.Strings;
//import org.apache.commons.lang3.math.NumberUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import reactor.core.Reactor;
//import reactor.spring.context.annotation.Consumer;
//import reactor.spring.context.annotation.ReplyTo;
//import reactor.spring.context.annotation.Selector;
//
//import javax.annotation.Resource;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//import java.util.concurrent.ThreadLocalRandom;
//
///**
// * 把后台方法分拆执行 暂时没有用
// *
// * @author yanghg@youyuan.com
// * @date 2014年8月8日
// * @description
// * @version 1.0
// */
//@Consumer
//public class UserPieceHandler {
//    @Resource
//    private Reactor preactor;
//    @Resource
//    private Reactor reactor;
//    @Resource
//    private MessageClientService messageClientService;
//
//
//    private static final Logger logger = LoggerFactory.getLogger(UserPieceHandler.class);
//
//    /**
//     * 根据用户ID查询用户的异步任务
//     *
//     * @param tranEvent
//     */
//
//    @Selector(value = PieceSelector.GET_USER_BY_ID, reactor = "@preactor")
//    @ReplyTo()
//    public void findByUserId(TranEvent<User> tranEvent) {
//        User user = userClientService.findById(NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId"))));
//        tranEvent.getPromise().accept(user);
//    }
//
//    /**
//     * 更新用户征友条件的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_UPDATE_MATCHTERM, reactor = "@preactor")
//    @ReplyTo()
//    public void updateMatchTerm(@SuppressWarnings("rawtypes") TranEvent tranEvent) {
//        userClientService.updateMatchTerm((MatcherTerm) tranEvent.getParam("MatcherTerm"));
//    }
//
//    /**
//     * 根据用户ID获取用户在线状态的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_ONLINE, reactor = "@preactor")
//    @ReplyTo()
//    public void userOnline(TranEvent<Map<Long, String>> tranEvent) {
//        long GUID = NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId")));
//        Map<Long, String> map = new HashMap<Long, String>();
//        map.put(GUID, userClientService.isOnline(GUID) ? "1" : "0");
//        tranEvent.getPromise().accept(map);
//    }
//
//    /**
//     * 根据用户ID和对方ID获取用户是否已关注的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_ISATTENTION, reactor = "@preactor")
//    @ReplyTo()
//    public void userIsAttention(TranEvent<Map<Long, String>> tranEvent) {
//        long GUID = NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId")));
//        long sendUserId = NumberUtils.toLong(Strings.toString(tranEvent.getParam("sendUserId")));
//        Map<Long, String> map = new HashMap<Long, String>();
//        map.put(sendUserId, customerService.isAttentionUser(GUID, sendUserId) ? "1" : "0");
//        tranEvent.getPromise().accept(map);
//    }
//
//    /**
//     * 根据用户ID是否对对方ID打过招呼的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_ISSAYHELLO, reactor = "@preactor")
//    @ReplyTo()
//    public void userIsSayHello(TranEvent<UserIsSayHi> tranEvent) {
//        long GUID = NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId")));
//        long toUserId = NumberUtils.toLong(Strings.toString(tranEvent.getParam("toUserId")));
//        UserIsSayHi userIsSayHi = new UserIsSayHi();
//        userIsSayHi.setToUserId(toUserId);
//        userIsSayHi.setIsSayHi(messageClientService.findSayHi(GUID, toUserId) ? 1 : 0);
//        tranEvent.getPromise().accept(userIsSayHi);
//    }
//
//    /**
//     * 根据用户ID和对方对象获取缘分VIEW的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_INITYUANFENVIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void initYuanfenView(TranEvent<YuanfenView> tranEvent) {
//        long GUID = NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId")));
//        User searchUser = (User) tranEvent.getParam("searchUser");
//        YuanfenView yuanfen = null;
//        try {
//            yuanfen = userClientService.initYuanfenView(GUID, searchUser);
//            yuanfen.getUserBase().getImage().setThumbnailUrl(UserUtil.userIconBySize(searchUser.getGUID(), searchUser, 117, 143, false));
//            yuanfen.getUserBase().getImage().setImageUrl(UserUtil.userIconBySize(searchUser.getGUID(), searchUser, 270, 330, false));
//        } catch (Exception e) {
//            logger.error(e.getMessage().toString());
//        }
//        tranEvent.getPromise().accept(yuanfen);
//    }
//
//    /**
//     * 根据用户ID和对方ID获取搜索VIEW的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_INITSEARCHVIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void initSearchView(TranEvent<SearchView> tranEvent) {
//        // 本人ID
//        long GUID = NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId")));
//        // 对方ID
//        long uid = NumberUtils.toLong(Strings.toString(tranEvent.getParam("uid")));
//        SearchView view = null;
//        try {
//            view = userClientService.initSearchView(uid);
//        } catch (Exception e) {
//            logger.error(e.getMessage().toString());
//        }
//        tranEvent.getPromise().accept(view);
//    }
//
//    /**
//     * 根据用户ID和对方ID获取搜索VIEW的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_INITNEARBYVIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void initNearbyView(TranEvent<NearbyUserView> tranEvent) {
//        User nearbyUser = (User) tranEvent.getParam("nearbyUser");
//        User user = (User) tranEvent.getParam("user");
//        NearbyUserView nearbyUserView = new NearbyUserView();
//        try{
//            nearbyUserView.setIsSayHello(messageClientService.findSayHi(user.getGUID(), nearbyUser.getGUID()) ? 1 : 0);
//            nearbyUserView.setUserBase(userClientService.initUserBaseView(nearbyUser));
//            nearbyUserView.setImgCount(nearbyUser.getPhotoCount());
//            nearbyUserView.setListLabel(initUserLabel(nearbyUser.getGUID()));
//        }
//        catch (Exception e){
//            logger.error(e.getMessage());
//        }
//
//        tranEvent.getPromise().accept(nearbyUserView);
//    }
//
//    /**
//     * 根据用户ID和对方ID获取消息VIEW的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_INITMSGBOXVIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void initMsgBoxView(TranEvent<MsgBoxView> tranEvent) {
//        Message message = (Message) tranEvent.getParam("message");
//        User sender = (User) tranEvent.getParam("sender");
//        User receiver = (User) tranEvent.getParam("receiver");
//        tranEvent.getPromise().accept(messageClientService.initMsgBoxView(message, sender, receiver));
//    }
//
//    /**
//     * 根据用户ID获取用户 BASE VIEW的异步任务
//     *
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_INITUSERBASEVIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void initUserBaseView(TranEvent<UserBaseView> tranEvent) {
//        tranEvent.getPromise().accept(userClientService.initUserBaseView(NumberUtils.toLong(Strings.toString(tranEvent.getParam("userId")))));
//    }
//
//    /**
//     * 根据用户对象获取用户 BASE VIEW的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_INITUSERBASEVIEWWITHUSER, reactor = "@preactor")
//    @ReplyTo()
//    public void initUserBaseViewWithUser(TranEvent<UserBaseView> tranEvent) {
//        tranEvent.getPromise().accept(userClientService.initUserBaseView((User) tranEvent.getParam("user")));
//    }
//
//    /**
//     * 根据用户ID和对方ID获取消息VIEW的异步任务
//     *
//     * @param tranEvent
//     */
//    @Selector(value = PieceSelector.USER_PUSHVISITLOG, reactor = "@reactor")
//    @ReplyTo()
//    public void pushVisitLog(TranEvent<MsgBoxView> tranEvent) {
//        long uid = NumberUtils.toLong(Strings.toString(tranEvent.getParam("uid")));
//        User currentUser = (User) tranEvent.getParam("currentUser");
//        // iOS互访缓存
//        String visitKey = CacheUtil.generateKey("IOS_HULU_VISIT", uid);
//        CacheUtil.putCache(visitKey, currentUser.getGUID(), 24 * 60 * 60);
//
//        // 推送互访通知
//        if (uid != currentUser.getGUID()) {
//            if (androidUserService.isHuluUser(uid)) {
//                PushMsgView pushMsgView = new PushMsgView();
//                pushMsgView.setType(2);
//                pushMsgView.setTitle("你有一位新访客");
//                pushMsgView.setText(currentUser.getNickname() + "正在查看你的空间");
//                pushMsgView.setUserBase(userClientService.initUserBaseView(currentUser));
//                // 轮询缓存
//                String key1 = CacheUtil.generateKey("VISIT-MESSAGE", uid);
//                CacheUtil.putCache(key1, pushMsgView, 1800); // 缓存30分钟
//            }
//            if (androidUserService.isIosHuluUser(uid)) {
//                String alert = currentUser.getNickname() + "正在查看你的空间";
//                Map<String, String> extras = new HashMap<String, String>();
//                extras.put("type", "2");
//                extras.put("senderId", String.valueOf(currentUser.getGUID()));
//                extras.put("url", "");
//                PushUtils.sendIOSMessage(alert, extras, true, 24 * 60 * 60, Long.toString(uid));
//            }
//        }
//    }
//
//    @Selector(value = PieceSelector.TWEET_VIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void tweetView(TranEvent<TweetView> tranEvent) {
//        @SuppressWarnings(value = "unchecked")
//        Map<String, Object> tweetDataMap = (Map<String, Object>) tranEvent.getParam("tweetDataMap");
//        long userId = NumberUtils.toLong(Objects.toString(tranEvent.getParam("userId"), ""));
//        int release = NumberUtils.toInt(Objects.toString(tranEvent.getParam("release"), ""));
//
//        tranEvent.getPromise().accept(ugcTweetClientService.initTweet(tweetDataMap, userId, release));
//    }
//
//    @Selector(value = PieceSelector.TWEET_MSG_VIEW, reactor = "@preactor")
//    @ReplyTo()
//    public void tweetMsgView(TranEvent<TweetMsgView> tranEvent) {
//        TweetMsg tweetMsg = (TweetMsg) tranEvent.getParam("tweetMsg");
//        tranEvent.getPromise().accept(ugcTweetClientService.initTweetMsg(tweetMsg));
//    }
//
//    private double distSimple(double dist, long fUid, long tUid) {
//        if (dist >= 0 && dist <= 10000) {
//            return dist;
//        }
//        String userID = String.valueOf(fUid);
//        String uID = String.valueOf(tUid);
//        int abs = Math.abs(Integer.parseInt(userID.substring(userID.length() - 2)) - Integer.parseInt(uID.substring(uID.length() - 2)));
//        // 如果相减之后结果为0，随机获取1-10之间一个数
//        if (abs == 0) {
//            abs = ThreadLocalRandom.current().nextInt(1, 10);
//        }
//        return abs * 1000;
//    }
//
//    private List<String> initUserLabel(long userId) {
//        UserMend userMend = userClientService.findMendById(userId);
//        List<String> list = Lists.newArrayList();
//        if (userMend == null)
//            return null;
//        String[] c = userMend.getCharacteristic().split(",");
//        if (c.length != 0) {
//            list.add(c[0]);
//        }
//        String[] i = userMend.getInterest().split(",");
//        if (i.length != 0) {
//            list.add(i[0]);
//        }
//        return list;
//    }
//}
